﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyHotel.MyHotel_MHPhieuThuePhong_BUS_Webservice; 

namespace MyHotel
{
    public partial class frmNhanPhong : MyForm
    {
        private Phong _phong;
        private PhieuThuePhong _phieuthue;
        private KhachHang _khachhang;

        public frmNhanPhong(MyHotel_MHPhong_BUS_Webservice.Phong p)
        {
            setPhong(p);
            InitializeComponent();
        }

        private void setPhong(MyHotel_MHPhong_BUS_Webservice.Phong p)
        {
            _phong = new Phong();
            _phong.MaPhong = p.MaPhong;
            _phong.TenPhong = p.TenPhong;
            _phong.TinhTrang = p.TinhTrang;
            _phong.TienPhong = p.TienPhong;
            _phong.GhiChu = p.GhiChu;
            _phong.LoaiPhong = p.LoaiPhong;
        }

        private void frmNhanPhong_Load(object sender, EventArgs e)
        {
            txtTenPhong.Text = _phong.TenPhong;
            inputTien.Text = _phong.TienPhong.ToString();

            dateTimeNgayVao.Text = DateTime.Now.ToShortDateString();
            dateTimeLuc.Text = DateTime.Now.ToShortTimeString();
        }

        private void txtTenKhachHang_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void inputCMND_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private bool IsDataOK()
        {
            if (txtTenKhachHang.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Vui lòng nhập tên khách hàng");
                txtTenKhachHang.Focus();
                return false;
            }

            return true;
        }

        private void CreateData()
        {
            _phieuthue = new PhieuThuePhong();
            _phieuthue.MaPhong = _phong.MaPhong;
            _phieuthue.NgayThue = dateTimeNgayVao.Value;
            _phieuthue.NgayTra = dateTimeNgayVao.Value;
            _phieuthue.DonGia = inputTien.Value; 

            _khachhang = new KhachHang();
            _khachhang.TenKhachHang = txtTenKhachHang.Text.Trim();
            _khachhang.DiaChi = txtDiaChi.Text.Trim();
            _khachhang.CMND = inputCMND.Value.ToString();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (IsDataOK())
            {
                MHPhieuThuePhongBUSWSSoapClient ws = new MHPhieuThuePhongBUSWSSoapClient();
                CreateData();
                if ( !ws.themPhieuThuePhong(_phieuthue, _khachhang))
                {
                    MessageBox.Show("Không thành công!!!", "Thông Báo");
                    return;
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            } 
        }

        private void dateTimeLuc_ValueChanged(object sender, EventArgs e)
        {
            dateTimeNgayVao.Value = new DateTime(    dateTimeNgayVao.Value.Year,
                                                     dateTimeNgayVao.Value.Month,
                                                     dateTimeNgayVao.Value.Day,
                                                     dateTimeLuc.Value.Hour,
                                                     dateTimeLuc.Value.Minute,
                                                     dateTimeLuc.Value.Second     );
        } 
    }
}
